package KFMmovie;

import java.util.Objects;
import java.util.Scanner;

public class Test {
    private static MovieTheater mov = new MovieTheater();
    private static User loginUser;
    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        menu();
    }

    /**
     * 主界面菜单
     */
    public static void menu() {
        System.out.println("*******************");
        System.out.println("1. 查询电影信息");
        System.out.println("2. 显示所有电影信息");
        System.out.println("3. 管理员登录");
        System.out.println("4. 用户登录");
        System.out.println("5. 用户注册");
        System.out.println("0. 退出");
        System.out.println("*******************");
        System.out.println("请输入操作：");
        int op = sc.nextInt();
        switch (op) {
            case 1 -> selectMovie();
            case 2 -> showMovie(0);
            case 3 -> userJoin(1);
            case 4 -> userJoin(2);
            case 5 -> addUser();
            case 0 -> {
                System.out.println("退出成功");
                return;
            }
        }
    }

    /**
     * 查询电影
     */
    public static void selectMovie() {
        System.out.println("请输入你要查找的电影：");
        String name = sc.next();
        Movie movie = mov.selectMovie(name);
        if (movie == null) {
            System.out.println("电影未上映！");
        } else {
            System.out.println(movie);
        }
        menu();
    }

    /**
     * 显示所有电影
     */
    public static void showMovie(int type) {
        mov.sortMovieByDate();
        if (type == 0) {
            menu();
        } else if (type == 1){
            menu1();
        }else if (type ==2){
            return;
        }
    }

    /**
     * 管理员与用户登录
     */
    public static void userJoin(int role) {
        System.out.println("请输入电话号码：");
        String phone = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();
        loginUser = mov.selectUSer(password, phone, role);
        if (loginUser == null) {
            System.out.println("登陆失败！");
            menu();
        } else {
            System.out.println("登陆成功！");
            System.out.println(loginUser.getUsername() + ",欢迎来到开发喵影院！");
            if (role == 1) {
                menu1();
            } else {
                menu2();
            }
        }
    }

    /**
     * 添加用户
     */
    public static void addUser() {
        System.out.println("请输入用户名：");
        String username = sc.next();
        System.out.println("请输入电话号码：");
        String phone = sc.next();
        // TODO 验证电话号码是否被注册了 电话号码长度在11位  length() 可以获取字符串的长度  数组中是 length 属性
        // 电话号码是11位
        // 验证电话号码是否已经被注册
        phone = checkPhone(phone);
        System.out.println("请输入密码：");
        String password = sc.next();
        System.out.println("请再次输入密码：");
        String rePass = sc.next();
        // 让两次输入的密码一致
        password = checkpassword(password, rePass);
        User user = new User(username, phone, password, 2);
        mov.addUser(user);
        System.out.println("注册成功！请登录！");
        menu();
    }

    /**
     * 检验电话是否为十一位，并且是否被注册
     *
     * @param phone 要检验的号码
     * @return 合理的号码
     */
    private static String checkPhone(String phone) {
        while (phone.length() != 11) {
            System.out.println("电话号码为十一位！请重新输入：");
            phone = sc.next();
        }
        while (mov.selectUserByPhone(phone) != null) {
            System.out.println("电话号码已被注册！请重新输入：");
            phone = sc.next();
        }
        return phone;
    }

    /**
     * 检验两次输入的密码是否一致
     *
     * @param psaaword 第一次
     * @param rePass   第二次
     * @return 返回一个一致的密码
     */
    private static String checkpassword(String psaaword, String rePass) {
        while (psaaword == null || !psaaword.equals(rePass)) {
            System.out.println("输入的两次密码不一致！请重新输入：");
            System.out.println("请输入密码：");
            psaaword = sc.next();
            System.out.println("请再次输入密码：");
            rePass = sc.next();
        }
        return psaaword;
    }

    /**
     * 管理员界面
     */

    public static void menu1() {
        System.out.println("*******************");
        System.out.println("1. 添加电影");
        System.out.println("2. 修改电影");
        System.out.println("3. 删除电影");
        System.out.println("4. 查看所有电影");
        System.out.println("5. 查看所有用户");
        System.out.println("0. 退出");
        System.out.println("*******************");
        System.out.println("请输入你的操作类型：");
        int op = sc.nextInt();
        switch (op) {
            case 1 -> addMovie();
            case 2 -> updateMovie();
            case 3 -> deleteMovie();
            case 4 -> showMovie(1);
            case 5 -> showUser(1);
            case 0 -> {
                System.out.println("退出成功");
                menu();
            }
        }
    }

    /**
     * 添加电影
     */
    public static void addMovie() {
        System.out.println("请输入添加的电影：");
        String name = sc.next();
        System.out.println("请输入价格：");
        double price = sc.nextDouble();
        System.out.println("请输入导演：");
        String director = sc.next();
        System.out.println("请输入上映时间：");
        int date = sc.nextInt();
        System.out.println("请输入票数：");
        int count = sc.nextInt();
        Movie movie = new Movie(name, price, director, date, count);
        mov.addMovie(movie);
        System.out.println("添加成功！" + movie.getName());
        menu1();
    }

    /**
     * 修改电影
     */
    public static void updateMovie() {
        System.out.println("请输入要修改的电影名称：");
        String name = sc.next();
        Movie movie = mov.selectMovie(name);
        while (movie == null) {
            System.out.println("修改的电影未上映！请重新输入：");
            name = sc.next();
            movie = mov.selectMovie(name);
        }
        if (name != null) {
            System.out.println("你要修改哪些信息？");
            System.out.println("1. 价格");
            System.out.println("2. 导演");
            System.out.println("3. 上映时间");
            System.out.println("0. 取消");
            int op = sc.nextInt();
            switch (op) {
                case 1 -> {
                    System.out.println("请输入价格：");
                    double pricce = sc.nextDouble();
                    movie.setPricce(pricce);
                    System.out.println("修改成功！");
                }
                case 2 -> {
                    System.out.println("请输入导演：");
                    String director = sc.next();
                    movie.setDirector(director);
                    System.out.println("修改成功！");
                }
                case 3 -> {
                    System.out.println("请输入上映时间：");
                    int date = sc.nextInt();
                    movie.setDate(date);
                    System.out.println("修改成功！");
                }
                case 0 -> {
                    menu1();
                }
            }
            menu1();
        }
    }

    /**
     * 删除电影
     */
    public static void deleteMovie() {
        System.out.println("请输入需要删除的电影");
        String name = sc.next();
        if (mov.deleteMovie(name) == true) {
            System.out.println("删除成功！");
        } else {
            System.out.println("删除失败，该电影未上映！");
        }
        menu1();
    }

    /**
     * 显示所有电影
     */
    public static void shouMovie() {
        mov.showMovies();
        menu1();
    }


    /**
     * 用户功能菜单
     */

    /**
     * 用户界面
     */
    public static void menu2() {
        System.out.println("*******************");
        System.out.println("1. 查看用户信息");
        System.out.println("2. 修改用户信息");
        System.out.println("3. 购买电影票");
        System.out.println("4. 显示购买信息");
        System.out.println("0. 退出");
        System.out.println("*******************");
        int type = sc.nextInt();
        switch (type) {
            case 1 -> showUser(2);
            case 2 -> updateUser();
            case 3 -> buyMovies();
            case 4 -> showBuyMovies();
            case 0 -> {
                System.out.println("退出成功");
                menu();
            }
        }
    }


    /**
     * 显示用户
     */
    public static void showUser(int type) {
        System.out.println(loginUser);
        if (type == 1) {
            menu1();
        } else {
            menu2();
        }
    }


    /**
     * 修改用户信息
     */

    public static void updateUser() {
        System.out.println("你要修改哪些信息？");
        System.out.println("1. 用户名");
        System.out.println("2. 密码");
        System.out.println("3. 电话");
        System.out.println("0. 取消");
        // loginUser 存储的是登录的用户信息
        int op = sc.nextInt();
        switch (op) {
            case 1 -> {
                System.out.println("请输入用户名：");
                String name = sc.next();
                loginUser.setUsername(name);
                System.out.println("修改成功！");
            }
            case 2 -> {
                System.out.println("请输入密码：");
                String password = sc.next();
                System.out.println("请再次输入密码：");
                String rePass = sc.next();
                // 让两次输入的密码一致
                password = checkpassword(password, rePass);
                loginUser.setPassword(password);
                System.out.println("修改成功！");
            }
            case 3 -> {
                System.out.println("请输入电话号码：");
                String phone = sc.next();
                // 电话号码是11位
                // 验证电话号码是否已经被注册
                phone = checkPhone(phone);
                loginUser.setPhone(phone);
                System.out.println("修改成功！");
            }
            case 0 -> {
                menu2();
            }
        }
        menu2();
    }

    /**
     * 购买电影票
     */
    public static void buyMovies() {
        showMovie(2);
        System.out.println("请输入你要购买的电影名称：");
        String name = sc.next();
        Movie movie = mov.buyMovieByName(name);
        System.out.println("请输入购买的票数：");
        int tickets = sc.nextInt();
        int count = movie.getCount(); // 当前电影所有票数
        while (tickets > count || tickets < 0) {
            System.out.println("输入的票数有问题，当前票总剩余：" + count);
            System.out.println("请重新输入：");
            tickets = sc.nextInt();
        }
        movie.setCount(count - tickets);
        loginUser.addMoive(new Movie(movie, tickets));
        System.out.println("购买成功");
        menu2();
    }

    /**
     * 显示购买信息
     */
    private static void showBuyMovies() {
        Movie[] buyMovies = loginUser.getBuyMovies();
        for (int i = 0; i < buyMovies.length; i++) {
            if (buyMovies[i] != null) {
                System.out.println(buyMovies[i]);
            }
        }
        menu2();
    }

}